package com.steveproject.parttimeworktree.utils;

import android.app.Activity;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.steveproject.parttimeworktree.R;

/**
 * 封装标题栏
 */
public class TitleBuilder {

    private View titleView;
    private RelativeLayout titleBar;
    private TextView title, rightTv;
    private ImageView leftIco, rightIco;

    /**
     * 构造函数
     *
     * @param act
     */
    public TitleBuilder(Activity act) {
        this.titleView = act.findViewById(R.id.ll_titleview);
        this.titleBar = act.findViewById(R.id.rl_titlebar);
        this.title = act.findViewById(R.id.title_mid);
        this.leftIco = act.findViewById(R.id.title_left);
        this.rightIco = act.findViewById(R.id.iv_title_right);
        this.rightTv = act.findViewById(R.id.tv_title_right);
    }

    /**
     * 设置标题栏文字
     *
     * @return
     */
    public TitleBuilder setTitleText(String titleStr) {
        this.title.setText(titleStr);
        return this;
    }

    /**
     * 用于设置标题栏左边要显示的图片
     */
    public TitleBuilder setLeftIco(int resId) {
        leftIco.setVisibility(resId > 0 ? View.VISIBLE : View.GONE);
        leftIco.setImageResource(resId);
        return this;
    }

    /**
     * 用于设置标题栏右边要显示的图片
     */
    public TitleBuilder setRightIco(int resId) {
        rightTv.setVisibility(View.GONE);
        rightIco.setVisibility(View.VISIBLE);
        rightIco.setImageResource(resId);
        return this;
    }

    /**
     * 用于设置标题栏右边要显示的文字
     *
     * @param rightStr
     * @return
     */
    public TitleBuilder setRightText(String rightStr) {
        rightIco.setVisibility(View.GONE);
        rightTv.setVisibility(View.VISIBLE);
        rightTv.setText(rightStr);
        return this;
    }

    /**
     * 设置标题右边文字的颜色
     *
     * @param resId
     * @return
     */
    public TitleBuilder setRightTextColor(int resId) {
        rightTv.setTextColor(resId);
        return this;
    }

    /**
     * 用于设置标题栏左边图片的单击事件
     */
    public TitleBuilder setLeftIcoListening(View.OnClickListener listener) {
        if (leftIco.getVisibility() == View.VISIBLE) {
            leftIco.setOnClickListener(listener);
        }
        return this;
    }

    /**
     * 用于设置标题栏右边图片的单击事件
     */
    public TitleBuilder setRightIcoListening(View.OnClickListener listener) {
        if (rightIco.getVisibility() == View.VISIBLE) {
            rightIco.setOnClickListener(listener);
        }
        return this;
    }

    public TitleBuilder setRightTextListening(View.OnClickListener listener) {
        if (rightTv.getVisibility() == View.VISIBLE) {
            rightTv.setOnClickListener(listener);
        }
        return this;
    }


}
